package com.study.leetcode;

//https://leetcode-cn.com/problems/count-and-say/
public class Solution_38 {
    public String countAndSay(int n) {
        if (n == 1) return "1";
        String pre = countAndSay(n - 1);
        StringBuilder ans = new StringBuilder();
        int count = 1;
        for (int i = 0; i < pre.length(); i++) {
            if (i + 1 >= pre.length() || pre.charAt(i) != pre.charAt(i + 1)) {
                ans.append(count);
                ans.append(pre.charAt(i));
                count = 0;
            }
            count++;
        }
        return ans.toString();
    }

    public static void main(String[] args) {
        Solution_38 solution_38 = new Solution_38();
        solution_38.countAndSay(5);
    }
}
